<%--
  Created by IntelliJ IDEA.
  User: YanHilin
  Date: 2020/4/23
  Time: 20:01
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<nav id="pageN" aria-label="Page navigation example">
    <%--大于首页，才显示--%>
    <ul class="pagination">
        <c:if test="${requestScope.page.pageNo>1}">
            <li class="page-item">
                <a class="page-link" href="${requestScope.page.url}&pageNo=1">首页</a>
            </li>
            <li class="page-item">
                <a class="page-link" href="${requestScope.page.url}&pageNo=${requestScope.page.pageNo-1}" aria-label="Previous">
                    <span aria-hidden="true">&laquo;</span>
                </a>
            </li>
        </c:if>
        <%--页码输出开始--%>
        <c:choose>
            <%--当前页码小于5--%>
            <c:when test="${requestScope.page.pageTotal<=5}">
                <c:set var="begin" value="1"></c:set>
                <c:set var="end" value="${requestScope.page.pageTotal}"></c:set>
            </c:when>
            <%--总页码大于5--%>
            <c:when test="${requestScope.page.pageTotal>5}">
                <c:choose>
                    <%--情况1：当前页码显示为1，2，3，范围为1-5--%>
                    <c:when test="${requestScope.page.pageNo<=3}">
                        <c:set var="begin" value="1"></c:set>
                        <c:set var="end" value="5"></c:set>
                    </c:when>
                    <%--情况2：当前页码范围为1-5--%>
                    <c:when test="${requestScope.page.pageNo>requestScope.page.pageTotal-3}">
                        <c:set var="begin" value="${requestScope.page.pageTotal-4}"></c:set>
                        <c:set var="end" value="${requestScope.page.pageTotal}"></c:set>
                    </c:when>
                    <%--情况3: 范围当前页码-2到当前页码+2--%>
                    <c:otherwise>
                        <c:set var="begin" value="${requestScope.page.pageNo-2}"></c:set>
                        <c:set var="end" value="${requestScope.page.pageNo+2}"></c:set>
                    </c:otherwise>
                </c:choose>
            </c:when>
        </c:choose>
        <c:forEach begin="${begin}" end="${end}" var="i">
            <c:if test="${i==requestScope.page.pageNo}">
                <li class="page-item active" aria-current="page">
                    <a class="page-link"> ${i} <span class="sr-only">(current)</span></a>
                </li>
            </c:if>
            <c:if test="${i !=requestScope.page.pageNo}">
                <li class="page-item"><a class="page-link" href="${requestScope.page.url}&pageNo=${i}">${i}</a></li>
            </c:if>
        </c:forEach>
        <%--页码输出结束--%>

        <%--当前页小于总页数才显示--%>
        <c:if test="${requestScope.page.pageNo<requestScope.page.pageTotal}">
            <li class="page-item">
                <a class="page-link" href="${requestScope.page.url}&pageNo=${requestScope.page.pageNo+1}" aria-label="Next">
                <span aria-hidden="true">&raquo;</span>
                </a>
            </li>
            <li class="page-item"><a class="page-link" href="${requestScope.page.url}&pageNo=${requestScope.page.pageTotal}">尾页</a></li>
        </c:if>
        <li class="page-item">
            <a class="page-link">
                共${requestScope.page.pageTotal}页，${requestScope.page.pageTotalCount}条结果
            </a>
        </li>
        <input style="height: 34px;width: 40px;border: none"  type="text" id="pageInput" value="${requestScope.page.pageNo}">
        <button type="button" id="searchPageBtn" style="background-color: white;color: black;border: none;height: 34px">跳转</button>
    </ul>
</nav>
<script type="text/javascript">
    $(function () {
        $("#searchPageBtn").click(function () {
            var pageNo=$("#pageInput").val();
            //获取总页数
            var pageTotal=${requestScope.page.pageTotal};
            if(pageNo<1||pageNo>pageTotal){
                alert(pageNo+"数值非法，请重新输入！");
            }else{
                location.href="${requestScope.basePath}${requestScope.page.url}&pageNo="+pageNo;
            }
        })
    })
</script>